/**
 * Created by luckyjing on 16/2/5.
 */

var user = require('../controllers/users.server.controller'),
    passport = require('passport'),
    Util = require('../util/util');
module.exports = function (app) {
    //注册
    app.route('/signup')
        .get(user.renderSignup)
        .post(user.signup);
    //登录

    app.route('/signin')
        .get(user.renderSignin)
        .post(function (req, res, next) {
            passport.authenticate('local', function (err, user, info) {
                if (err) {
                    return res.json({
                        message: Util.getErrorMessage(err)
                    });
                }
                if (!user) {
                    return res.json({
                        status: 1001,
                        message: "用户名或密码错误"
                    });
                }
                req.login(user, function (err) {
                    if (err) {
                        return next(err);
                    }
                    return res.json({
                        status: 1000,
                        message: "登陆成功"
                    });
                });
            })(req, res, next)
        });
    app.get('/signout', user.logout);


    app.route('/users')
        .post(user.create)
        .get(user.list);


    app.route('/users/:userId')
        .get(user.read)
        .post(Util.requireLogin, user.update);

    app.route('/users/delete/:userId')
        .post(Util.requireLogin, user.delete);

    app.route('/userInfo')
        .get(Util.requireLogin, user.info);

    app.param('userId', user.userByID);
};